package com.cz.nongyetongv5.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.dialog.CustomDialog;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;
import com.cz.nongyetongv5.utils.HttpUtils;
import com.cz.nongyetongv5.utils.HttpUtils.OnRequestExecuted;

public class YiJianFanKuiActivity extends Activity implements OnClickListener {

	/**
	 * 退出的按钮
	 */
	private ViewGroup btnBack = null;

	// 反馈内容的文本
	private EditText edtFanKuiNeiRong = null;

	// 反馈提交的按钮
	private Button btnSubmitFanKui = null;

	// 反馈成功
	private static final int FANKUI_SUCCESS = 1;
	// 服务器错误
	private static final int FANKUI_SERVER_ERROR = 0;
	// 网络异常
	private static final int FANKUI_NO_NET = -1;

	// 程序出错
	private static final int FANKUI_SOFT = -2;

	// 反馈结果,默认为反馈成功
	private int upload_result = 1;

	// 反馈的handler
	private Handler uploadFanKuiHandler = null;

	private User user;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.yijianfankui);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		edtFanKuiNeiRong = (EditText) findViewById(R.id.edtFanKuiNeiRong);
		edtFanKuiNeiRong.setOnClickListener(this);

		btnSubmitFanKui = (Button) findViewById(R.id.btnSubmitFanKui);
		btnSubmitFanKui.setOnClickListener(this);

		user = (User) getIntent().getSerializableExtra("user");

		uploadFanKuiHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {

				switch (msg.what) {
					case FANKUI_NO_NET:
						showMessageDialog(getString(R.string.error_no_network));
						break;
					case FANKUI_SERVER_ERROR:
						showMessageDialog(getString(R.string.message_userinfo_servererror));
						break;
					case FANKUI_SOFT:

						showMessageDialog(getString(R.string.message_softerror));

						break;
					case FANKUI_SUCCESS:

						edtFanKuiNeiRong.setText("");

						showMessageDialog(getString(R.string.message_fankui_success));
						break;
				}

				super.handleMessage(msg);

			}

		};

		loadRightTitle();
	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {
		if (v == btnBack) {
			this.finish();
		}

		// 点击提交的时候
		if (v == btnSubmitFanKui) {
			String fankuiNeiRong = this.edtFanKuiNeiRong.getText().toString()
					.trim();

			if (CharsetUtil.isNotNull(fankuiNeiRong)) {

				final String url = new ConfigUtil(getResources())
						.getPreHttpUrl()
						+ getResources().getString(R.string.upload_fankui);

				try {
					final JSONObject params = new JSONObject();
					params.put("key", fankuiNeiRong);

					final HttpUtils httpUtil = HttpUtils.getInstance();

					// 当返回的时候
					httpUtil.setOnRequestExecuted(new OnRequestExecuted() {
						@Override
						public void onRequestExecuted(int responseCode,
													  String responseBody) {
							// 说明网络不通
							if (responseCode < 0) {
								upload_result = FANKUI_NO_NET;
							}

						}
					});

					// 启动上传
					new Thread() {

						@Override
						public void run() {
							httpUtil.sendRequest(url, params.toString(),
									"POST", "UTF-8", null, null, user);
						};

					}.start();

				} catch (JSONException e) {
					// 程序出错
					upload_result = FANKUI_SOFT;
				}

				uploadFanKuiHandler.sendEmptyMessage(upload_result);

			}

		}

	}

	/**
	 * 弹出信息框
	 */
	private void showMessageDialog(String message) {

		final CustomDialog messageDialog = new CustomDialog(this);
		messageDialog.setMessage(message);

		messageDialog.setOnActionClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				messageDialog.dismiss();
			}
		});

		messageDialog.setActionTxt(getResources().getString(
				R.string.jiandujiancha_btn_sureitem));

		messageDialog.show();

	}

}
